﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using System.ComponentModel;
using System.Threading;
using System.Data;
using System.Web;

namespace MixNP.BusinessLayer
{
    public class EmailProcessor
    {
        bool invalid = false;

        public void Send(string fromDisplayName, string sendTo, string subject, string body, string id)
        {
            if (!(string.IsNullOrWhiteSpace(sendTo) || string.IsNullOrWhiteSpace(fromDisplayName) || string.IsNullOrWhiteSpace(subject) || string.IsNullOrWhiteSpace(body)))
            {
                string from = fromDisplayName;
                if (string.IsNullOrWhiteSpace(from))
                {
                    from = "MixNP Classifieds";
                }

                string[] addresses = sendTo.Split(',');
                foreach (string address in addresses)
                {
                    if (!IsValidEmail(address))
                    {
                        return;
                    }
                }

                addresses = addresses.Distinct().ToArray();
                sendTo = string.Join(",", addresses);

                //Todo:update email credentials here
                string host = "smtp.gmail.com";
                int port = 587;
                bool enableSsl = true;
                string userName = "@gmail.com";
                string password = "password";

                Send(from, sendTo, subject, body, host, port, enableSsl, userName, password, id);
            }
        }

        private static void Send(string from, string recipients, string subject, string body, string host, int port, bool enableSsl, string userName, string password, string id)
        {
            ThreadPool.QueueUserWorkItem(callback =>
            {
                MailAddress sender = new MailAddress(userName, from);

                using (MailMessage mail = new MailMessage(userName, recipients))
                {
                    mail.From = sender;
                    using (SmtpClient smtp = new SmtpClient(host, port))
                    {
                        smtp.EnableSsl = enableSsl;
                        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential(userName, password);
                        try
                        {
                            mail.Subject = subject;
                            mail.Body = body;
                            mail.IsBodyHtml = true;
                            mail.SubjectEncoding = System.Text.Encoding.UTF8;
                            smtp.Send(mail);
                            MixNP.DatabaseLayer.Email.FlagSent(id);
                        }
                        catch (SmtpException)
                        {
                        }
                    }
                }
            });
        }

        public bool IsValidEmail(string strIn)
        {
            invalid = false;

            if (String.IsNullOrWhiteSpace(strIn))
            {
                return false;
            }

            // Use IdnMapping class to convert Unicode domain names.
            strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper);
            if (invalid)
            {
                return false;
            }

            // Return true if strIn is in valid e-mail format.
            return Regex.IsMatch(strIn,
                   @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                   @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$",
                   RegexOptions.IgnoreCase);
        }

        private string DomainMapper(Match match)
        {
            // IdnMapping class with default property values.
            IdnMapping idn = new IdnMapping();

            string domainName = match.Groups[2].Value;
            try
            {
                domainName = idn.GetAscii(domainName);
            }
            catch (ArgumentException)
            {
                invalid = true;
            }
            return match.Groups[1].Value + domainName;
        }

    }
}
